﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Comsenz.Base
{
    /// <summary>
    /// 转换器
    /// </summary>
    /// <author>Chiron</author>
    public class Converter
    {
        /// <summary>
        /// 转为数值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToInt(string value)
        {
            try
            {
                return int.Parse(value);
            }
            catch
            {
                throw new Exception(string.IsNullOrEmpty(value) ? "不能将空值转换成 int." : value);
            }
        }

        /// <summary>
        /// 转为列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<T> ToList<T>(string value)
            where T : class
        {
            var result = XmlHelper.UnSerialize(value);

            if (result != null)
            {
                return result.Select(d => d.Value as T).ToList();
            }
            throw new Exception(string.IsNullOrEmpty(value) ? "不能将空值转换成列表 ." : value);
        }

        /// <summary>
        /// 转为字典
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> ToDict<TKey, TValue>(string value)
            where TKey : class
            where TValue : class
        {
            var result = XmlHelper.UnSerialize(value);
            if (result != null)
            {
                return result.ToDictionary(d => d.Key as TKey, d => d.Value as TValue);
            }
            throw new Exception(string.IsNullOrEmpty(value) ? "不能将空值传成字典 ." : value);
        }

        /// <summary>
        /// 转为字典列表
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<Dictionary<TKey, TValue>> ToDictList<TKey, TValue>(string value)
            where TKey : class
            where TValue : class
        {
            var result = XmlHelper.UnSerialize(value);
            if (result != null)
            {
                try
                {
                    return result.Select(d =>
                    {
                        var tValue = d.Value as Dictionary<object, object>;
                        if (tValue != null) return tValue.ToDictionary(e => e.Key as TKey, e => e.Value as TValue);
                        return null;
                    }).Where(d => d != null).ToList();
                }
                catch { }
            }
            throw new Exception(string.IsNullOrEmpty(value) ? "不能将空值转换成字典列表." : value);
        }
    }
}
